package com.terren.spider.db.basic;

import javax.annotation.Resource;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class BasicServiceSupportImpl implements IBasicServiceSupport {

	@Resource(name = "basicDaoSupport")
	protected IBasicDaoSupport basicDao;
	@Resource(name = "transactionManager")
	protected DataSourceTransactionManager transactionManager;

	public TransactionStatus startTransaction() {
		// 获取事务定义
		DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
		// 设置事务的传播行为，此处是设置为开启一个新事务
		definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
		// 设置事务的隔离级别，此处是读已经提交
		definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
		//
		// 获取事务状态对象
		TransactionStatus transactionStatus = (TransactionStatus) transactionManager.getTransaction(definition);
		return transactionStatus;
	}
	public void commitTransaction(TransactionStatus transactionStatus) {
		if (transactionStatus!=null&&!transactionStatus.isCompleted()) {
			//提交事务
	        transactionManager.commit(transactionStatus);
		}
	}
}
